<?php

namespace App\Http\Controllers;

use App\Activity;
use App\StudentFile;
use Illuminate\Http\Request;
use App\College;

class HomeController extends Controller
{
    function index(){
        $storage = config('app.storage_path');
        $colleges = College::whereNotNull("image")->inRandomOrder()->limit(4)->get()->toArray();
        return view('welcome',[
            'storage' => $storage,
            'collegesR1' => array_slice($colleges, 0,2),
            'collegesR2' => array_slice($colleges, 2,2),
        ]);
    }

    function seniors(){
        $seniors = StudentFile::inRandomOrder()->limit(20)->get();
        //return response()->json($seniors);
        $storage = config('app.storage_path');
        return view('seniors', [
            'storage' => $storage,
            'seniors' => $seniors
        ]);
    }

    function college($id){
        $storage = config('app.storage_path');
        $college =  College::find($id);
        return view('college',[
            'storage' => $storage,
            'college' => $college,
        ]);
    }

    function activity($id){
        $storage = config('app.storage_path');
        $activity =  Activity::find($id);
        return view('activity',[
            'storage' => $storage,
            'activity' => $activity,
        ]);
    }


    function colleges(){
        $storage = config('app.storage_path');
        $re = College::whereNotNull("image")->get();
        $colleges = [];
        for($i = 0; $i < count($re); $i++){
            $colleges[floor($i/3)][] = $re[$i];
        }
        $coldColleges = College::where("is_cold", 1)->orderBy('rank', 'asc')->get();
        return view('colleges',[
            'storage' => $storage,
            'colleges' => $colleges,
            'coldColleges' => $coldColleges
        ]);
    }

    function activities(){
        $storage = config('app.storage_path');
        $re = Activity::orderBy('created_at', 'desc')->get();
        $activities = [];
        for($i = 0; $i < count($re); $i++){
            $activities[floor($i/2)][] = $re[$i];
        }
        return view('activities',[
            'storage' => $storage,
            'activities' => $activities
        ]);
    }
}
